/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Listholiday.java
 *
 * Created on Jul 14, 2011, 11:26:18 PM
 */
package manager;

import generalCode.Connectionsql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author kaka
 */
public class Listholiday extends javax.swing.JFrame {

    private ArrayList<HolidayDTO> listHoliday = new ArrayList<HolidayDTO>();
    Vector<String> columnNames;

    {
        columnNames = new Vector<String>();
        //columnNames.add("Id");
        columnNames.add("EmployeeID");
        columnNames.add("Name");
        columnNames.add("Begin Date");
        columnNames.add("End Date");
        columnNames.add("Description");


    }
    private Object id;

    public Listholiday() {
        this.setTitle("Holiday");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        initComponents();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        initData();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNameholiday = new javax.swing.JTextField();
        txtdescription = new javax.swing.JTextField();
        add = new javax.swing.JButton();
        btReset = new javax.swing.JButton();
        Delete = new javax.swing.JButton();
        startDay = new javax.swing.JTextField();
        endDay = new javax.swing.JTextField();
        btcancel = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        tfID = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        JEmployeeID = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbholiday = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Holiday's Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 255, 0))); // NOI18N

        jLabel1.setText("Name holiday:");

        jLabel2.setText("Begin date :");

        jLabel3.setText("End date:");

        jLabel4.setText("Description:");

        txtNameholiday.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameholidayActionPerformed(evt);
            }
        });

        add.setText("Add");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        btReset.setText("Reset");
        btReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btResetActionPerformed(evt);
            }
        });

        Delete.setText("Delete");
        Delete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                DeleteMouseClicked(evt);
            }
        });
        Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });

        btcancel.setText("Cancel");
        btcancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btcancelMouseClicked(evt);
            }
        });
        btcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcancelActionPerformed(evt);
            }
        });

        jLabel5.setText("ID:");

        tfID.setEditable(false);

        jLabel6.setText("Employee id:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtdescription, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(txtNameholiday, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(endDay, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(startDay, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(tfID, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(JEmployeeID, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(add)
                        .addGap(18, 18, 18)
                        .addComponent(Delete)
                        .addGap(18, 18, 18)
                        .addComponent(btReset)))
                .addGap(18, 18, 18)
                .addComponent(btcancel)
                .addGap(80, 80, 80))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfID, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(JEmployeeID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNameholiday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(startDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(endDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtdescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(add)
                    .addComponent(Delete)
                    .addComponent(btReset)
                    .addComponent(btcancel))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Holiday's list"));

        tbholiday.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Employee id", "Name holiday", "Begin date", "end date", "Description"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbholiday.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbholidayMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbholiday);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNameholidayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameholidayActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNameholidayActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed

        insertHoliday();
    }//GEN-LAST:event_addActionPerformed

    private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed
        String Id = this.tfID.getText();
        int response = JOptionPane.showConfirmDialog(null, "Are you sure delete it?", "Confirm",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (response == JOptionPane.YES_OPTION) 
                deleteHoliday(Id);           
    }//GEN-LAST:event_DeleteActionPerformed

    private void btResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btResetActionPerformed
        resetForm();
    }//GEN-LAST:event_btResetActionPerformed

    private void tbholidayMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbholidayMouseClicked
        showInfor();
    }//GEN-LAST:event_tbholidayMouseClicked

    private void btcancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btcancelMouseClicked
        this.setVisible(false);
        Managerform1 work = new Managerform1();
        work.setVisible(true);
    }//GEN-LAST:event_btcancelMouseClicked
//block code for cancel button
    private void btcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btcancelActionPerformed
        this.dispose();
      
        ;
    }//GEN-LAST:event_btcancelActionPerformed

    //block for delete
    private void DeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DeleteMouseClicked
    }//GEN-LAST:event_DeleteMouseClicked
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Delete;
    private javax.swing.JTextField JEmployeeID;
    private javax.swing.JButton add;
    private javax.swing.JButton btReset;
    private javax.swing.JButton btcancel;
    private javax.swing.JTextField endDay;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField startDay;
    private javax.swing.JTable tbholiday;
    private javax.swing.JTextField tfID;
    private javax.swing.JTextField txtNameholiday;
    private javax.swing.JTextField txtdescription;
    // End of variables declaration//GEN-END:variables

    private void initData() {

    listHoliday = new ArrayList<HolidayDTO>();
        this.tbholiday.removeAll();
        try {
            //load db
            Connectionsql db = Connectionsql.newInstance();
            String sql = "select * from holiday";
            ResultSet rs = db.query(sql);
            while (rs.next()) {
                listHoliday.add(new HolidayDTO(rs.getString("HolidayID"),rs.getInt("EmployeeID"), rs.getString("NameHoliday"), rs.getString("BeginDay"), rs.getString("EndDay"), rs.getString("Description")));
            }
            db.closeConn();
            //show table
            Vector<Vector<String>> data = new Vector<Vector<String>>();
            for (int i = 0; i < listHoliday.size(); i++) {
                Vector<String> v = new Vector<String>();
                v.add(listHoliday.get(i).getId() + "");
                v.add(listHoliday.get(i).getEmployeeID()+"");
                v.add(listHoliday.get(i).getName());
                v.add(listHoliday.get(i).getStartDate());
                v.add(listHoliday.get(i).getEndDate());
                v.add(listHoliday.get(i).getDescription());
                //
                data.add(v);

            }
            tbholiday.setModel(new DefaultTableModel(data, columnNames));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    private void showInfor() {
        int selectedRow = -1;
        selectedRow = tbholiday.getSelectedRow();
        if (selectedRow != -1) {
            tfID.setText(listHoliday.get(selectedRow).getId() + "");
            JEmployeeID.setText(listHoliday.get(selectedRow).getEmployeeID()+"");
            txtNameholiday.setText(listHoliday.get(selectedRow).getName());
            startDay.setText(listHoliday.get(selectedRow).getStartDate());
            endDay.setText(listHoliday.get(selectedRow).getEndDate());
            txtdescription.setText(listHoliday.get(selectedRow).getDescription());
        }
    }

    private void insertHoliday() {

        if (validateInput()) {
            try {
                Connectionsql db = Connectionsql.newInstance();
                // insert into values (1, '', '')
                String sql = "insert into holidays values('"+getText(tfID)
                        + getText(JEmployeeID)
                        + getText(txtNameholiday)
                        + "','" + getText(startDay)
                        + "', '" + getText(endDay)
                        + "','" + getText(txtdescription)
                        + "')";
                System.out.println(sql);
                if (db.update(sql)) {
                    JOptionPane.showMessageDialog(this, "add new holiday successfully....");
                    this.tfID.setText("");
                    this.JEmployeeID.setText("");
                    this.txtNameholiday.setText("");
                    this.startDay.setText("");
                    this.endDay.setText("");
                    this.txtdescription.setText("");
                    initData();
                }

            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "ERROR: " + ex.getMessage());
            }

        }

    }

    private boolean validateInput() {
        if (txtNameholiday.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Input id please");
            return false;
        }
        return true;
    }

    private String getText(JTextField txt) {
        return txt.getText().trim();
    }

    public void deleteHoliday(String id) {
        try {
            String sql = "delete holidays where id = ?;";
            Object[] arr = new Object[]{id};
            Connectionsql conn = Connectionsql.newInstance();
            if (conn.updateWithParam(sql, arr)) {
                JOptionPane.showMessageDialog(this, "the holiday has delete Successfully");
                 this.tfID.setText("");
                 this.JEmployeeID.setText("");
                    this.txtNameholiday.setText("");
                    this.startDay.setText("");
                    this.endDay.setText("");
                    this.txtdescription.setText("");
                initData();
                conn.closeConn();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Listholiday.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void resetForm() {
        this.tfID.setText("");
        this.JEmployeeID.setText("");
        this.txtNameholiday.setText("");
        this.txtdescription.setText("");
        this.endDay.setText("");
        this.startDay.setText("");
    }
}
